package com.frank.towerrush.util;

import java.util.ArrayList;

import com.frank.towerrush.R;
import com.frank.towerrush.service.SoundService;
import com.frank.towerrush.split.Tower;
import com.frank.towerrush.util.SystemParameter.SystemString;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;

/**
 * 系统方法
 * 
 * @author Frank
 *
 */
public final class SystemMethod {
    private SystemMethod() {
    }

    /**
     * 系统初始化
     * 
     * @param context
     *            上下文
     */
    public static void systemInit(Context context) {
        initScreen(context);
        initTower(context);
        loadPreferences(context);
        initSound(context);
    }

    /**
     * 初始化塔
     * 
     * @param context
     *            上下文
     */
    private static void initTower(Context context) {
        SystemParameter.towers = new ArrayList<Tower>();

        String[] towersName = context.getResources().getStringArray(R.array.help_tower_name);
        String[] towersInfo = context.getResources().getStringArray(R.array.help_tower_info);
        int[] towersImage = explainIntegerArray(context, R.array.help_tower_image);

        for (int i = 0; i < towersImage.length; i++) {
            Bitmap image = BitmapFactory.decodeResource(context.getResources(), towersImage[i]);
            SystemParameter.towers.add(new Tower(towersName[i], towersInfo[i], image, null, 0, 0));
        }
    }

    /**
     * 初始化声音
     * 
     * @param context
     *            上下文
     */
    private static void initSound(Context context) {
        context.startService(new Intent(context, SoundService.class));
        SystemParameter.soundService = SoundService.getService();
        if (SystemParameter.soundOn) {
            SystemParameter.soundService.musicStart(context, SystemParameter.SystemAudio.MUSIC_PLAY, true);
        }
    }

    /**
     * 初始化屏幕
     * 
     * @param context
     *            上下文
     */
    private static void initScreen(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(dm);
        SystemParameter.screenHeight = dm.heightPixels;
        SystemParameter.screenWidth = dm.widthPixels;
    }

    /**
     * 读取偏好设置
     * 
     * @param context
     *            上下文
     */
    private static void loadPreferences(Context context) {
        SharedPreferences spf = context.getSharedPreferences(SystemString.PREFERENCES_PATH, Context.MODE_PRIVATE);
        SystemParameter.soundOn = spf.getBoolean(SystemString.PREFERENCES_SOUND_ON, true);
        SystemParameter.soundVolume = spf.getInt(SystemString.PREFERENCES_SOUND_VOLUME, 5);
        SystemParameter.musicVolume = spf.getInt(SystemString.PREFERENCES_MUSIC_VOLUME, 5);
        SystemParameter.hreat = spf.getInt(SystemString.PREFERENCES_HREAT, 20);
        SystemParameter.gold = spf.getInt(SystemString.PREFERENCES_GOLD, 200);
    }

    /**
     * 保存偏好设置
     * 
     * @param context
     *            上下文
     */
    public static void savePreferences(Context context) {
        SharedPreferences spf = context.getSharedPreferences(SystemString.PREFERENCES_PATH, Context.MODE_PRIVATE);
        Editor editor = spf.edit();
        editor.putBoolean(SystemString.PREFERENCES_SOUND_ON, SystemParameter.soundOn);
        editor.putInt(SystemString.PREFERENCES_SOUND_VOLUME, SystemParameter.soundVolume);
        editor.putInt(SystemString.PREFERENCES_MUSIC_VOLUME, SystemParameter.musicVolume);
        editor.putInt(SystemString.PREFERENCES_HREAT, SystemParameter.hreat);
        editor.putInt(SystemString.PREFERENCES_GOLD, SystemParameter.gold);
        editor.commit();
    }

    /**
     * 读取资源中的数组
     * 
     * @param context
     * @param id
     * @return
     */
    public static int[] explainIntegerArray(Context context, int id) {
        TypedArray ta = context.getResources().obtainTypedArray(id);

        int[] array = new int[ta.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = ta.getResourceId(i, 0);
        }
        ta.recycle();
        return array;
    }
}
